package com.atguigu.gmall.common.execption;

import com.atguigu.gmall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author:lyd
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(GmallException.class)
    public Result handleGmallException(GmallException e) {
        Result<Object> result = new Result<>();
        result.setCode(e.getCode());
        result.setMessage(e.getMessage());
        return result;
    }

    @ExceptionHandler(Throwable.class)
    public Result handleThrowable(Throwable e) {
        Result<Object> result = new Result<>();
        result.setCode(500);
        result.setMessage(e.getMessage());
        log.error("全局异常处理,错误信息：[{}]", e.getMessage());
        return result;
    }
}
